﻿--[[
   @brief    tab menu
   @author   hqzheng
   @version  1.0
   @date     2017-04-19
--]]


tab_menu = 
{
    menu_id = 0;
    MENU_HEIGHT = 134;
    menu_h = 0;
    item_info_list = nil;
    item_id_2_info = {};
    module_id = 0;

    init = function (parent_id, item_info_list, module_id)
        if nil == item_info_list then
            return;
        end
        tab_menu.item_info_list = item_info_list;
        tab_menu.module_id = module_id;

        tab_menu.menu_id = parent_id;
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil == menu_ptr then
            return;
        end
        menu_ptr:RemoveAll();

        local item_count = table.getn(item_info_list);
        for k, v in pairs(item_info_list) do
            if nil ~= v and nil ~= v.item_text then
                local item_id = 0;
                if 1 == k then
                    item_id = tab_menu.add_tab_menu_top(menu_ptr, v.item_text);
                elseif item_count == k then
                    item_id = tab_menu.add_tab_menu_btm(menu_ptr, v.item_text);
                else
                    item_id = tab_menu.add_tab_nor_item(menu_ptr, v.item_text);
                end
                ui_utility.setproperty_by_id(item_id, "OnClick", "tab_menu.on_item_click");
                tab_menu.item_id_2_info[item_id] = v;
            end
        end

        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil ~= menu_ptr then
            local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);
            tab_menu.menu_h = menu_rc.bottom - menu_rc.top;
        end
    end;

    show = function (parent_id, is_show, off_set_x, off_set_y)
        -- tab_menu.init(parent_id);
        if is_show then
            -- 移动到要显示的位置
            local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
            if nil ~= menu_ptr then
                local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);
                local menu_w = menu_rc.right - menu_rc.left;
                local menu_h = menu_rc.bottom - menu_rc.top;
                menu_rc.left = off_set_x;
                menu_rc.right = menu_rc.left + menu_w;
                menu_rc.top = off_set_y;
                menu_rc.bottom = menu_rc.top;

                menu_ptr:MoveEx(menu_rc,TRUE);
            end
            tab_menu.show_animation(parent_id);
        else
            tab_menu.hide_animation(parent_id);
        end
    end;

    add_tab_menu_top = function (menu_ptr, item_name)
        local item_id = 0;
        if nil ~= menu_ptr then
            local const_ui_str = U8toCUIStr("wegame_mainframe\\MenuItem.tpf/TenioUILayout/UIObject/Children/UIObject?btnShopTop");
            item_id = menu_ptr:AppendItem(const_ui_str);
        end
        ui_utility.setproperty_by_id(item_id, "Caption", item_name);
        return item_id;
    end;

    add_tab_menu_btm = function (menu_ptr, item_name)
        local item_id = 0;
        if nil ~= menu_ptr then
            local const_ui_str = U8toCUIStr("wegame_mainframe\\MenuItem.tpf/TenioUILayout/UIObject/Children/UIObject?btnShopBtm");
            item_id = menu_ptr:AppendItem(const_ui_str);
        end
        ui_utility.setproperty_by_id(item_id, "Caption", item_name);
        return item_id;
    end;

    add_tab_nor_item = function (menu_ptr, item_name)
        local item_id = 0;
        if nil ~= menu_ptr then
            local const_ui_str = U8toCUIStr("wegame_mainframe\\MenuItem.tpf/TenioUILayout/UIObject/Children/UIObject?btnShopItem");
            item_id = menu_ptr:AppendItem(const_ui_str);
        end
        ui_utility.setproperty_by_id(item_id, "Caption", item_name);
        return item_id;
    end;

    on_item_click = function (uilib, control_id)
        local item_info = tab_menu.item_id_2_info[control_id];
        if nil == item_info or nil == item_info.open_url then
            return;
        end
        if 1 == tonumber(item_info.open_type) then
            -- tgp浏览器打开
            local param = {};
            param.type = "open_web";
            param.url = item_info.open_url;
            param.browser = "new";
            local json_str = base_utility.table_to_json(param);
            web_utility.open_link(json_str);
        else
            -- 页面打开
            tab_menu.jump_sub_page(tab_menu.module_id, item_info.open_url);
        end
    end;

    show_animation = function (menu_id)
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        if nil == menu_ptr then
            return;
        end

        menu_ptr:Show(TRUE);

        local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);

        -- 开始尺寸
        local resize_start_sz = SIZE:new_local();
        resize_start_sz.cx = menu_rc.right - menu_rc.left;
        resize_start_sz.cy = 0;

        -- 展开后的尺寸
        local resize_end_sz = SIZE:new_local();
        resize_end_sz.cx = menu_rc.right - menu_rc.left;
        resize_end_sz.cy = tab_menu.menu_h;

        base_utility.log("[tab_menu]show menu animation");
        -- 缩放动画
        animation_utility.init(
            tab_menu.menu_id,
            "Shop_Menu_Resize", animation_utility.ANI_TYPE.ANI_SIZE);
        animation_utility.size_animation(
            tab_menu.menu_id, resize_start_sz, resize_end_sz, 6, 0);

    end;

    hide_animation = function (menu_id)
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        if nil ~= menu_ptr then
            base_utility.log("[tab_menu]hide menu");
            menu_ptr:Show(FALSE);
        end
    end;

    jump_sub_page = function (game_id, url)
        local param = {};
        param.type = "jump_url";
        param.id = game_id;
        param.url = url;
        param.op_from = 0;
        local json_str = base_utility.table_to_json(param);
        web_utility.open_link(json_str);
    end;
};

